requirejs(['jquery', 'pageJs','config', 'arttemplateUtil','common','validform','wdatePicker'], function($, pageJs,config, arttemplateUtil,common) {
	var param = {tab:1};
	var initData = function(){
		common.loading();
		common.common_ajax_request('/api/v1/message_list',param,true,function(data,code,msg){
			if(msg.businessCode==0){
				setData(data);
				pageJs.setPage(data.pageNo,data.totalPage,data.totalCount,callback);
				initBtn();
			}else{
				setData({});
				window.layer.msg("系统异常");
			}
			layer.closeAll('loading'); //关闭加载层
		},"","post");
	};
	
	var msglist_tpl;//列表模板
	var initTpl = function(){
		msglist_tpl = arttemplateUtil.template.compile($.setHTML('message/tpl/msg_list_tpl.html'));
	};
	
	//列表
	var setData = function(data){
		var res = {
				tab:param.tab,
				records:data.records,
				url:config.imgRoot()
		}
		$("#msglist").html(msglist_tpl(res));
	};
	//分页回调
	var callback = function(page){
		param.pageNo=page;
		initData();
	};
	//初始化
	var init = function() {
		initTpl();
		initData();
		arttemplateUtil.auload();
	};
	//查询验证
	var validserchform = $("#serchForm").Validform({
		tiptype:4,
		postonce:true,
		isShowRight:false
	});
	
	//页签选择,更换页签需要清空查询条件
	$('#tabs-632335 a').click(function (e) {
		  param.tab=$(this).attr("tab_value");
		  param.pageNo=1;
		  initData();
	});
	/*
	$("#serchForm input[name=first_message_date]").on('click',function(){
		var date_param = {el:'first_message_date'};
		
		if($("#serchForm input[name=last_message_date]").val() != undefined && $("#serchForm input[name=last_message_date]").val() !=""){
			date_param.maxDate=$("#serchForm input[name=last_message_date]").val();
		}
		WdatePicker(date_param);
	});
	$("#serchForm input[name=last_message_date]").on('click',function(){
		var date_param = {el:'last_message_date'};
		if($("#serchForm input[name=first_message_date]").val() != undefined && $("#serchForm input[name=first_message_date]").val() !=""){
			date_param.minDate=$("#serchForm input[name=first_message_date]").val();
		}
		WdatePicker(date_param);
	});
*/
  //查询
  $('.serchBtn').on('click',function(){
	  if(validserchform.check()){
		param.pageNo=1;
		$.each($('#serchForm').serializeArray(), function(i, item) {
			if(item.value==""){
				delete param[item.name];
			}else{
				param[item.name]=item.value;
			}
		});
		initData();
  	  }
   });
   //重置
	$('.resetBtn').on('click',function(){
		$.each($('#serchForm').serializeArray(), function(i, item) {
			delete param[item.name];
		});
		$("#serchForm select[name=module_id]").val("");
		$("form[name=serchForm] input").val("");
		$("#serchForm input").removeClass("Validform_error");
		$("#serchForm .Validform_checktip").removeClass("Validform_wrong").html("");
		initData();
	});

	var initBtn = function(){
		$(".handle-btn").click(function(){
			var id = $(this).attr("record-id");
			layer.confirm('确定处理吗？', {
				  btn: ['确定','取消']
			}, function(){
				common.loading();
				var param = {id: id};
				common.common_ajax_request('/api/v1/message_handle',param,false,function(data,code,msg){
					if(code==200 && msg.businessCode==0){
						initData();
						layer.alert("处理成功!");
					}else{
						layer.alert("处理失败");
					}
				},"","post");
			}, function(){});
		});
		
		$(".del-btn").click(function(){
			var id = $(this).attr("record-id");
			layer.confirm('确定删除吗？', {
				  btn: ['确定','取消']
			}, function(){
				common.loading();
				var param = {id: id};
				common.common_ajax_request('/api/v1/message_del',param,false,function(data,code,msg){
					if(code==200 && msg.businessCode==0){
						initData();
						layer.alert("删除成功!");
					}else{
						layer.alert("删除失败");
					}
				},"","post");
			}, function(){});
		});
	};
	init();
});
